package com.gensdai.leliang.common.database;

import android.content.Context;

import com.gensdai.leliang.AppApplication;
import com.tencent.wcdb.database.SQLiteDatabase;
import com.tencent.wcdb.database.SQLiteOpenHelper;
import com.tencent.wcdb.support.Log;

/**
 * Created by zsxx on 2017/6/28.
 */

public class EncryptedDBHelper extends SQLiteOpenHelper {
    private static final String TAG = "EncryptedDBHelper";

    private static final int DATABASE_VERSION = 1;

    private Context mContext;
    private String mPassphrase;

    public EncryptedDBHelper(Context context, String passphrase) {
        // 调用“加密”版本的超类构造函数。
        super(context, AppApplication.DATABASE_NAME, passphrase.getBytes(), null, null, DATABASE_VERSION,
                null);
        // 保存上下文对象供以后使用。
        mContext = context;
        mPassphrase = passphrase;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "Creating new encrypted database.");

        // 如果旧数据库不存在，请进行真正的初始化。
        db.execSQL("CREATE TABLE message (content TEXT, "
                + "sender TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, String.format("Upgrading database from version %d to version %d.",
                oldVersion, newVersion));

        //将新列添加到数据库升级的消息表中。
        db.execSQL("ALTER TABLE message ADD COLUMN sender TEXT;");
    }
}
